$Query = function (className, tag, el) {
    el = el || document;
    var elList = [];
    var classes = [];
    var classesToCheck = null;
    var returnElements = [];
    if (document.getElementsByClassName) {
        elList = el.getElementsByClassName(className);
        var nodeName = (tag) ? new RegExp("\\b" + tag + "\\b", "i") : null;
        var current;
        for (var i = 0; i < elList.length; i++) {
            current = elList[i];
            if (!nodeName || nodeName.test(current.nodeName)) {
                returnElements.push(current);
            }

        }
        return returnElements;
    }
    else if (document.evaluate) {
        tag = tag || "*";
        classes = className.split(" ");
        classesToCheck = "";
        var xhtmlNamespace = "http://www.w3.org/1999/xhtml";
        var namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace) ? xhtmlNamespace : null;
        var node;
        for (var j = 0, jl = classes.length; j < jl; j += 1) {
            classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
        }
        try {
            elList = document.evaluate(".//" + tag + classesToCheck, el, namespaceResolver, 0, null);
        }
        catch (e) {
            elList = document.evaluate(".//" + tag + classesToCheck, el, null, 0, null);
        }
        while ((node = elList.iterateNext())) {
            returnElements.push(node);
        }
        return returnElements;

    }
    else {
        tag = tag || "*";
        classes = className.split(" ");
        classesToCheck = [];
        elList = (tag === "*" && el.all) ? el.all : el.getElementsByTagName(tag);
        var match;
        for (var k = 0, kl = classes.length; k < kl; k += 1) {
            classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
        }
        for (var l = 0, ll = elList.length; l < ll; l += 1) {
            current = elList[l];
            match = false;
            for (var m = 0, ml = classesToCheck.length; m < ml; m += 1) {
                match = classesToCheck[m].test(current.className);
                if (!match) {
                    break;
                }
            }
            if (match) {
                returnElements.push(current);
            }
        }
        return returnElements;

    }
};
SetOpacity = function (el,val) {
    val = parseFloat(val);
    if (!isNaN(val)) {
        val = Math.max(Math.min(val, 1), 0);    
        if (window.ActiveXObject) {
            val = Math.ceil(val * 100);
            el.style.filter = "alpha(opacity=" + val + ")";
        }
        else {
            el.style.opacity = val;
        };
        return this;
    }
};
function UpdateRealTimeData() {
    this.rTable = GetDomObj("tbl_realtimeData");
    this.titleList = $Query("title", "span", this.rTable);
    this.clicksList = $Query("clicks", "span", this.rTable);
    this.linkList = $Query("detail", "a", this.rTable);
    this.emList = $Query("new", "em", this.rTable);
    this.realtimedata_update = 0;
    this.currRow = 0;
    this.data = null;

    var owner = this;
    this.ajax = new Ajax(function (data) {
        data = eval("(" + data + ")");
        if (owner.realtimedata_update != data.updateTime) {
            owner.data = data.data;
            owner.change();
            owner.realtimedata_update = data.updateTime;
        }
    });
    var req = this.ajax.request;
    
    req.onreadystatechange = function () {
        if (req.readyState != 4) {
            return;
        }

        if (req.status >= 200 && req.status < 300 || req.status == 304 || req.status == 0) {
	        owner.ajax.callback(req.responseText);
        } else {
            //alert(req.status);
        }
    };
    this.Send = function () {
        this.ajax.request.open("GET", "json.php?p=top10&reqTime=" + new Date().getTime(), true);
        this.ajax.request.setRequestHeader("Content-Type", "application/text;charset=utf-8");
        this.ajax.request.send(null);
    }
};


UpdateRealTimeData.prototype.fillData = function () {
    var data = this.data[this.currRow];
    var t = data.title;
    var h = 0;
    for (var e = 0; e < t.length; e++) {
        if (t.charCodeAt(e) < 128) {
            h += 1
        } else {
            h += 2
        }
        if (h >= 16) {
            t = t.substr(0, e - 1) + "...";
            break
        }
    }
    this.titleList[this.currRow].innerHTML = "<a href='" + data.urlPara + "' target='_blank' title='" + data.title + "'>" + t + "</a>" + (data.isNew == "true" ? "<em class='new'></em>" : "");
    this.linkList[this.currRow].setAttribute("href", data.detailUrl);
    this.clicksList[this.currRow].innerHTML = data.clicks;
    this.show();
};

UpdateRealTimeData.prototype.show = function () {
    var val = 0;
    var owner = this;
    var intervalshow = window.setInterval(function () {
        SetOpacity(owner.titleList[owner.currRow], val);
        SetOpacity(owner.linkList[owner.currRow], val);
        SetOpacity(owner.clicksList[owner.currRow], val);
        if (val >= 1) {
            window.clearInterval(intervalshow);
            //next row
            if (owner.currRow < owner.titleList.length - 1) {
                owner.currRow++;
                owner.change();
            }
            else {
                owner.currRow = 0;
            }
        }
        val += 0.2;
    }, 80);

    
};

UpdateRealTimeData.prototype.change = function () {
    var val = 1;
    var owner = this;
    var intervalhide = window.setInterval(function () {
        SetOpacity(owner.titleList[owner.currRow], val);
        SetOpacity(owner.linkList[owner.currRow], val);
        SetOpacity(owner.clicksList[owner.currRow], val);

        if (val <= 0) {
            window.clearInterval(intervalhide);
            owner.fillData();
        }
        val -= 0.2;
    }, 80);
    
};

var rdR = new UpdateRealTimeData();
window.setInterval("rdR.Send();", 180000);
